package com.erp.web.controller.system;

import com.erp.framework.annotation.Log;
import com.erp.framework.core.controller.BaseController;
import com.erp.framework.core.domain.AjaxResult;
import com.erp.framework.core.page.TableDataInfo;
import com.erp.framework.enums.BusinessType;
import com.erp.framework.util.AutoFillUtils;
import com.erp.framework.util.StringUtils;
import com.erp.system.domain.Vendor;
import com.erp.system.service.BsProvinceService;
import com.erp.system.service.VendorService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
  * FileName: VendorController.java
  * SpringMVC控制器的详细说明
  * @Date    创建日期
  * @version 1.00
*/
@Controller
@RequestMapping("/system/vendor")
@SuppressWarnings({ "rawtypes", "unchecked" })
public class VendorController extends BaseController {
    @Autowired
	private VendorService vendorService;

    private final String prefix = "system/vendor";

    @RequiresPermissions("system:vendor:view")
    @GetMapping()
    public String config() {
        return prefix + "/vendor";
    }

    @RequiresPermissions("system:vendor:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Vendor vendor) {
        startPage();
        List<Vendor> list = vendorService.queryAll(vendor);
        return getDataTable(list);
    }

    /**
     * 新增应商主数据
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增供应商主数据
     */
    @RequiresPermissions("system:vendor:add")
    @Log(title = "新增供应商主数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Vendor vendor) {
        if (vendorService.checkCodeUnique(vendor)) {
            return error("供应商编码已存在，请检查！");
        }
        AutoFillUtils.setCreateInfo(vendor);
        return toAjax(vendorService.insertVendor(vendor));
    }

    /**
     * 修改供应商跳转页面
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("vendor", vendorService.queryById(id));
        return prefix + "/edit";
    }

    /**
     * 更新保存供应商主数据
     */
    @RequiresPermissions("system:vendor:edit")
    @Log(title = "更新供应商主数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Vendor vendor) {
        if (vendorService.checkCodeUnique(vendor)) {
            return error("供应商编码已存在，请检查！");
        }
        AutoFillUtils.setEditInfo(vendor);
        return toAjax(vendorService.updateVendor(vendor));
    }

    @RequiresPermissions("system:vendor:remove")
    @Log(title = "删除供应商主数据", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String[] ids) {
        return toAjax(vendorService.deleteVendorByIds(ids));
    }

    /**
     * 校验编码
     */
    @PostMapping("/checkCodeUnique")
    @ResponseBody
    public boolean checkCodeUnique(Vendor vendor) {
        return StringUtils.isNotBlank(vendor.getCode()) && vendorService.checkCodeUnique(vendor);
    }


    @Autowired
    private BsProvinceService bsProvinceService;


    @PostMapping("/AllBsProvince")
    @ResponseBody
    public AjaxResult AllBsProvince() {
        return AjaxResult.success(bsProvinceService.AllBsProvince());
    }

    @PostMapping("/AllBsCity")
    @ResponseBody
    public AjaxResult AllBsCity(String provinceCode) {
        return AjaxResult.success(bsProvinceService.AllBsCity(provinceCode));
    }

    @PostMapping("/AllBsArea")
    @ResponseBody
    public AjaxResult AllBsArea(String cityCode) {
        return AjaxResult.success(bsProvinceService.AllBsArea(cityCode));
    }

    @PostMapping("/AllBsStreet")
    @ResponseBody
    public AjaxResult AllBsStreet(String areaCode) {
        return AjaxResult.success(bsProvinceService.AllBsStreet(areaCode));
    }
}

